//
//  HTTPRequest.m
//
//  Created by 刘川 on 2018/05/23.
//  Copyright © 2017年 com.YiQiLe. All rights reserved.
//

#import "HTTPRequest.h"
#import "NetworkManager.h"
#import "RequestApiConfig.h"


@implementation HTTPRequest

#pragma mark - 请求的公共方法
+ (void)requestType:(RequestType)type url:(NSString *)url params:(NSDictionary *)params httpRequestBlock:(httpRequestBlock) httpRequestBlock{
    
    NSLog(@"url---%@%@,  params--%@",kApi_Base_Url, url, params);

    //  配置请求头
    if (!([url isEqualToString:kApi_sendCode] || [url isEqualToString:kApi_login])) {
        
        NSLog(@"token---%@",[SJZAccountModel userFromArachiver].token);
        [[NetworkManager sharedManager].requestSerializer setValue:[SJZAccountModel userFromArachiver].token forHTTPHeaderField:@"token"];
    }
    [NetworkManager requestType:type url:url params:params callbackBlock:^(id response, NSError *error) {
        
        NSLog(@"response - %@\n errror - %@", response, error);
        
        if (error)
        {
            httpRequestBlock(nil,response[@"resultCode"],[self p_errorHandlingWithError:error]);
        }
        else
        {
            [self p_ApiResponseHandlingWithResponse:response httpRequestBlock:httpRequestBlock];
        }
    }];
}

/** 状态码判断处理 */
+ (void)p_ApiResponseHandlingWithResponse:(id) response httpRequestBlock:(httpRequestBlock) httpRequestBlock{
    
    if ([response[@"resultCode"] integerValue] == 0)
    {
        httpRequestBlock(response[@"resultData"],response[@"resultCode"],nil);
    }
    else if ([response[@"resultCode"] integerValue] == 6)
    {
        [SJZAccountModel saveUserObjectWithUser:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLoginStatusChange object:nil];
        httpRequestBlock(nil,response[@"resultCode"],response[@"resultDesc"]);
    }
    else
    {
        httpRequestBlock(nil,response[@"resultCode"],response[@"resultDesc"]);
    }
}

/** 错误信息处理,返回给用户提示信息 */
+ (NSString *)p_errorHandlingWithError:(NSError *) error{
    if (error.code == -1001)
    {
        return @"请求超时或网络异常";
    }
    else if(error.code == -1011)
    {
        return @"服务器数据异常";
    }
    else if(error.code == -1009)
    {
        return @"已断开与互联网的连接";
    }
    return @"请求失败";
}


#pragma -mark url配置
/** 发送验证码 */
+ (void)sendCodeWithParames:(NSDictionary *) params
           httpRequestBlock:(httpRequestBlock) httpRequestBlock{
    [self requestType:RequestTypePost url: kApi_sendCode params:params httpRequestBlock:httpRequestBlock];
}

/** 登录 */
+ (void)loginWithParames:(NSDictionary *) params
        httpRequestBlock:(httpRequestBlock) httpRequestBlock{
     [self requestType:RequestTypePost url: kApi_login params:params httpRequestBlock:httpRequestBlock];
}

/** 分页查询核销的兑换券 */
+ (void)listConsumeVoucherWithParames:(NSDictionary *) params
        httpRequestBlock:(httpRequestBlock) httpRequestBlock{
    [self requestType:RequestTypePost url: kApi_listConsumeVoucher params:params httpRequestBlock:httpRequestBlock];
}

/** 统计核销的兑换券 */
+ (void)listStatConsumeVoucherWithParames:(NSDictionary *) params
        httpRequestBlock:(httpRequestBlock) httpRequestBlock{
    [self requestType:RequestTypePost url: kApi_listStatConsumeVoucher params:params httpRequestBlock:httpRequestBlock];
}

/** 核销兑换券 */
+ (void)consumeVoucherWithParames:(NSDictionary *) params
        httpRequestBlock:(httpRequestBlock) httpRequestBlock{
    [self requestType:RequestTypePost url: kApi_consumeVoucher params:params httpRequestBlock:httpRequestBlock];
}

@end
